/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/toolbox"
	"github.com/valyala/fasthttp"

	//. "saas_sys_base/modules/api/models"
	. "saas_sys_base/modules/api/service"
)

type ApiCtrl struct {
	service        ApiService
}

func init() {
	vc := new(ApiCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *ApiCtrl) CtrlInfo() string {
	return "ApiCtrl"
}

func (vc *ApiCtrl) RoutInit() {


	lego.APIPOSTv2("/api/user/test", vc.Test, false)

	//
	lego.APIGET("/api/statistics/get_req_stastistics", vc.GetRequestStati, false)
	lego.APIGET("/api/statistics/clear_req_stastistics", vc.ClearRequestStati, false)

	lego.APIGET("/api/statistics/get_timestamp_result", vc.GetTimestampResult, false)
}


/*
type TestReq struct {
	A int  `json:"A,omitempty"`
	B int
}*/
type TestReq struct {
	A int  `json:"A, required"`
	B int
}

func (vc *ApiCtrl) Test(ctx *fasthttp.RequestCtx, req *TestReq) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	//data := make(map[string]interface{})

	code := 0
	msg := "success"

	for {
		//rawcode := string(ctxExt.Peek("code"))
		//tid, _ := utils.StringToInt64(string(ctxExt.Peek("tid"))) //tenant id
		//shareid := string(ctx.FormValue("shareid")) // 谁邀请的
		//rawData := string(ctx.PostArgs().Peek("rawData"))


		response["data"] = req
		break
	}

	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
	return
}


func (vc *ApiCtrl) GetRequestStati(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	//data := make(map[string]interface{})

	code := 0
	msg := "success"

	for {
		//rawcode := string(ctxExt.Peek("code"))
		//tid, _ := utils.StringToInt64(string(ctxExt.Peek("tid"))) //tenant id
		//shareid := string(ctx.FormValue("shareid")) // 谁邀请的
		//rawData := string(ctx.PostArgs().Peek("rawData"))


		response["data"] = toolbox.StatisticsMap.GetMapData()
		break
	}

	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
	return
}

func (vc *ApiCtrl) ClearRequestStati(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	//data := make(map[string]interface{})

	code := 0
	msg := "success"

	for {
		//rawcode := string(ctxExt.Peek("code"))
		//tid, _ := utils.StringToInt64(string(ctxExt.Peek("tid"))) //tenant id
		//shareid := string(ctx.FormValue("shareid")) // 谁邀请的
		//rawData := string(ctx.PostArgs().Peek("rawData"))


		toolbox.StatisticsMap.ClearMapData()
		break
	}

	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
	return
}



func (vc *ApiCtrl) GetTimestampResult(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	//data := make(map[string]interface{})

	code := 0
	msg := "success"

	for {
		//rawcode := string(ctxExt.Peek("code"))
		//tid, _ := utils.StringToInt64(string(ctxExt.Peek("tid"))) //tenant id
		//shareid := string(ctx.FormValue("shareid")) // 谁邀请的
		//rawData := string(ctx.PostArgs().Peek("rawData"))
		response["data"] = toolbox.GetTimestampResult()
		break
	}

	response["code"] = code
	response["msg"] = msg
	//response["data"] = data
	ctxExt.JSON(200, response)

	return
}